package com.imooc.distributedemo.resume.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * leetcode238 除自身外数组的乘积
 */
public class Solution238 {
    public static void main(String[] args) {
//        int[] nums = {1,2,3,4};
        int[] nums = {1,2,0,4};
        int[] array = productExceptSelf(nums);
        System.out.println(array);
    }

    public static int[] productExceptSelf(int[] nums) {
        int count = 0, idx = 0, pro = 1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                count++;
                idx = i;
            } else {
                pro *= nums[i];
            }
        }

        for (int i = 0; i < nums.length; i++) {
            if (count > 1) {
                return new int[nums.length];
            } else if (count == 1) {
                int[] res = new int[nums.length];
                res[idx] = pro;
                return res;
            } else {
                nums[i] = pro / nums[i];
            }
        }

        return nums;
        /*if(null==nums||nums.length<1){
            return null;
        }
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            List<Integer> integers = new ArrayList<>();
            for (int j = i; j < nums.length; j++) {
                int a
            }
        }
        return null;*/
    }
}
